package org.example.hbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.List;

/**
 * @author zhl
 * @version 1.0
 * @ClassName
 * @date 2020/11/6 14:32
 */
public class ExampleForHBase {
    public static Configuration configuration;
    public static Connection connection;
    public static Admin admin;

    public static ExampleForHBase getInstance = new ExampleForHBase();

    static {
        configuration  = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", "mnode1:2181");
        try{
            connection = ConnectionFactory.createConnection(configuration);
            admin = connection.getAdmin();
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    private ExampleForHBase(){}

    // 列出所有表
    public static void listTables() throws IOException {
        System.out.println("-------------------listTables-----------------");
        List<TableDescriptor> list = admin.listTableDescriptors();
        for (TableDescriptor tableDescriptor : list) {
            System.out.println(tableDescriptor.getTableName());
        }
    }

    // 列出指定表的所有列
    public static void listAllColumns(String name) throws IOException {
        System.out.println("----------------listAllColumns---------------");
        Table table = connection.getTable(TableName.valueOf(name));
        TableDescriptor hTableDescriptor=table.getDescriptor();
        for(ColumnFamilyDescriptor fdescriptor : hTableDescriptor.getColumnFamilies()){
            System.out.println(fdescriptor.getNameAsString());
        }
    }

    public static void close(){
        System.out.println("close");
        try{
            if(admin != null){
                admin.close();
            }
            if(null != connection){
                connection.close();
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }


    public static void createTable(String myTableName,String[] colFamily) throws IOException {
        System.out.println("-----------------create table------------------");
        TableName tableName = TableName.valueOf(myTableName);
        if (admin.tableExists(tableName)) {
            System.out.println("table is exists!");
        } else {
            TableDescriptorBuilder tableDescriptor = TableDescriptorBuilder.newBuilder(tableName);
            for (String str : colFamily) {
                ColumnFamilyDescriptor family = ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(str)).build();
                tableDescriptor.setColumnFamily(family);
            }
            admin.createTable(tableDescriptor.build());
        }
    }

    public static void insertData(String tableName,String rowKey,String colFamily,String col,String val) throws IOException {
        System.out.println("--------------------insert------------------");
        Table table = connection.getTable(TableName.valueOf(tableName));
        Put put = new Put(rowKey.getBytes());
        put.addColumn(colFamily.getBytes(), col.getBytes(), val.getBytes());
        table.put(put);
        table.close();
    }


    public static void getData(String tableName,String rowKey,String colFamily, String col)throws  IOException{
        System.out.println("------------------getData-----------------");
        Table table = connection.getTable(TableName.valueOf(tableName));
        Get get = new Get(rowKey.getBytes());
        get.addColumn(colFamily.getBytes(),col.getBytes());
        Result result = table.get(get);
        System.out.println(new String(result.getValue(colFamily.getBytes(), col.getBytes())));
        table.close();
    }

    public static void main(String[] args) throws IOException {
        listTables();
        listAllColumns("student");
//        createTable("student", new String[]{"score", "sex", "name"});
//        insertData("student","zhangsan","score","English","69");
//        insertData("student","zhangsan","score","Math","86");
//        insertData("student","zhangsan","score","Computer","77");
        getData("student", "zhangsan", "score","English");
        close();
    }
}
